package FileStudy;

import java.io.*;
import java.util.Scanner;

/**
 * 文件的拷贝
 */
public class Fdemo9 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要拷贝的源路径:");
        String src = scanner.next();
        System.out.println("请输入要拷贝的目标路径:");
        String dest = scanner.next();
        File srcFile = new File(src);
        if(!srcFile.isFile()){
            System.out.println("输入的源路径不正确!");
            return;
        }
        //读取源文件，拷贝到目标文件中
        try(InputStream inputStream = new FileInputStream(src)){
            try(OutputStream outputStream = new FileOutputStream(dest)){
                //把inputStream中的数据读出来，写入到outputStream中
                while (true){
                    byte[] buffer = new byte[1024];
                    int len = inputStream.read(buffer);
                    if(len == -1){
                        break;
                    }
                    //写入的时候，不能把整个buffer都写进去，可能只有一部分数据是有效的
                    outputStream.write(buffer,0,len);
                }
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
